﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace comm.objst
{
    #region MessageResult
    [Serializable]
    public enum MessageResultType
    {
        NORMAL = 0,
        SUCC = 1,
        FAIL = 2
    }

    [Serializable]
    public class MessageResult
    {
        public bool IsSucc
        {
            get { return Type == MessageResultType.SUCC; }
            set { Type = value ? MessageResultType.SUCC : MessageResultType.FAIL; }
        }

        public MessageResultType Type { get; set; }

        string _message = null;
        public string Message
        {
            get
            {
                if (_message == null)
                {
                    _message = "";
                }
                return _message;
            }
            set { _message = value; }
        }
    }
    #endregion

    #region TcpMessage
    [Serializable]
    public class TcpMessage
    {
        public string MsgId { get; set; }
        public TcpMessageType Type { get; set; }
        public object Obj { get; set; }
        public long SendTime { get; set; }
        public long TimeOffset { get; set; }
        public void TryToResult(Action<MessageResult> callback)
        {
            TryToObject<MessageResult>(callback);
        }
        public void TryToObject<T>(Action<T> callback)
        {
            T t = ToObject<T>();
            if (t is T)
            {
                callback(t);
            }
            else
            {
                Console.WriteLine("TcpMessage TryToObject<" + typeof(T) + ">() err! Type:" + Type + " Obj:" + Obj + " msgId:" + MsgId);
            }
        }
        public T ToObject<T>()
        {
            if (Obj is T)
            {
                return (T)Obj;
            }
            return default(T);
        }
    }
    #endregion

    [Serializable]
    public enum TcpMessageType
    {
        ToServerGetServerTime = 0,            //long[] ()
        ToServerGetHeroTypeList = 1,          //string[] ()
        ToServerGetStarList = 2,              //StarInfo[] ()
        ToServerGetStarFactionList = 3,       //FactionInfo[] (StarId)
        ToServerGetAllFactionDic = 6,         //Dictionary<string, comm.objst.FactionInfo[]> ()
        ToServerGetMyHeroList = 4,            //HeroBaseInfo[] (UserId)
        ToServerCreateHero = 5,               //MessageResult (CreateHero)

        ToServerGetHeroInfo = 100,              //HeroInfo (PlayerId)
        ToServerGetMyHeroInfo = 101,            //HeroFullInfo ()
        ToServerGetMyHeroEquipList = 102,       //EquipInfo[] ()
        ToServerGetMyBackpackList = 103,        //BackpackInfo[] ()
        ToServerGetMyStateList = 104,           //State[] ()

        ToServerGetMyUnitList = 200,            //UnitInfo[] ()
        ToServerGetMyUnitReadyList = 201,       //UnitReadyInfo[] ()
        ToServerGetUnitEquipList = 202,         //EquipInfo[] (UnitId)

        ToServerGetStarSpaceList = 300,         //SpaceInfo[] (StarId, AreaId)

        ToServerHeroJoinWorld = 400,            //MessageResult (PlayerId)
        ToServerClientReady = 401,              //PlayerId ()
        ToServerSpaceObjectBeginMove = 402,     //MoveToResult[] (MoveTo[])

        ToClientCallback = 1000,

        ToClientCreateSpace = 1001,             //void (MapId, SpaceId)
        ToClientSpaceFillObject = 1002,         //void (SObjectInfo[])
        ToClientSpaceObjectEnter = 1003,        //void (SObjectInfo[])
        ToClientSpaceObjectLeave = 1004,        //void (ObjectId[])
        ToClientSpaceObjectBeginMove = 1005,    //void (MoveTo[])
    }
}